;   Copyright (c) 2019 Renesas Electronics Corporation. All rights reserved.
;
;   Warranty Disclaimer :
;
;   Because the Product(s) is licensed free of charge, there is no warranty of
;   any kind whatsoever and expressly disclaimed and excluded by Renesas, 
;   either expressed or implied, including but not limited to those for non-
;   infringement of intellectual property, merchantability and/or fitness for
;   the particular purpose. Renesas shall not have any obligation to maintain,
;   service or provide bug fixes for the supplied Product(s) and/or the 
;   Application.
;
;   Each User is solely responsible for determining the appropriateness of using
;   the Product(s) and assumes all risks associated with its exercise of rights
;   under this Agreement, including, but not limited to the risks and costs of
;   program errors, compliance with applicable laws, damage to
;   or loss of data, programs or equipment, and unavailability or interruption
;   of operations.
;
;   Limitation of Liability :
;
;   In no event shall Renesas be liable to the User for any incidental,
;   consequential, indirect, or punitive damage (including but not limited to
;   lost profits) regardless of whether such liability is based on breach of
;   contract, tort, strict liability, breach of warranties, failure of essential
;   purpose or otherwise and even if advised of the possibility of such damages.
;   Renesas shall not be liable for any services or products provided by third
;   party vendors, developers or consultants identified or referred to the User
;   by Renesas in connection with the Product(s) and/or the Application.
;
;-----------------------------------------------------------------------------
;   processing module setting table
;-----------------------------------------------------------------------------

    .section    ".text.cmn", text
    .public     _pm1_setting_table
    .align      4
_pm1_setting_table:

    .dw         #__start_pm1            ; ENTRY ADDRESS

;-----------------------------------------------------------------------------
;    system stack
;-----------------------------------------------------------------------------

STACKSIZE    .set    0x200

    .section    ".stack.bss", bss
    .align      4
    .ds        (STACKSIZE)
    .align      4
_stacktop_pm1:

;-----------------------------------------------------------------------------
;   RAM section initialize table
;-----------------------------------------------------------------------------

    ; when the section has the initial value
    .section    ".INIT_DSEC.const", const
    .align      4
    .dw         #__s.data,  #__e.data,  #__s.data.R

    ; when the section without initial value
    .section    ".INIT_BSEC.const", const
    .align      4
    .dw         #__s.bss,   #__e.bss

;-----------------------------------------------------------------------------
;    startup
;-----------------------------------------------------------------------------

    .section    ".text.cmn", text
    .public     __start_pm1
    .align      4
__start_pm1:

    mov         #_stacktop_pm1, sp      ; set sp register
    mov         #__gp_data, gp          ; set gp register
    mov         #__ep_data, ep          ; set ep register

    ; initialize RAM area
    mov         #__s.INIT_DSEC.const, r6
    mov         #__e.INIT_DSEC.const, r7
    mov         #__s.INIT_BSEC.const, r8
    mov         #__e.INIT_BSEC.const, r9
    jarl32      __INITSCT_RH, lp        ; initialize RAM area

    ; enable FPU
    stsr        5, r10, 0               ; get PSW
    movhi       0x0001, r0, r11         ; set 1 in PSW.CU0 for enable FPU
    or          r11, r10
    ldsr        r10, 3, 0               ; set PSW via FEPSW

;    movhi       0x0002, r0, r11         ; set 1 in FPSR.FS for 
;    ldsr        r11, 6, 0               ; initialize FPSR
;    ldsr        r0, 7, 0                ; initialize FPEPC

    ; set various flags to PSW via FEPSW
;    stsr        5, r10, 0               ; get PSW
;    xori        0x0020, r10, r10        ; enable interrupt
;    movhi       0x4000, r0, r11
;    or          r11, r10                ; set 1 in PSW.UM for changing to user mode
;    ldsr        r10, 3, 0               ; set PSW via FEPSW

    mov         #_exit_pm1, lp          ; set _exit_pm1 address to lp
    mov         #_main_pm1, r10
    ldsr        r10, 2, 0               ; set _main_pm1 address to PC via FEPC

    feret                               ; apply PSW and PC

_exit_pm1:
    br          _exit_pm1               ;  end of program

;-----------------------------------------------------------------------------
;   abort
;-----------------------------------------------------------------------------

    .public     _abort_pm1
    .align      4

_abort_pm1:
    br          _abort_pm1

;-----------------------------------------------------------------------------
;    dummy section
;-----------------------------------------------------------------------------

    .section    ".data", data
.L.dummy.data:
    .section    ".bss", bss
.L.dummy.bss:
    .section    ".const", const
.L.dummy.const:
    .section    ".text", text
.L.dummy.text:

